package com.power.using.mybomb.myapp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.power.using.mybomb.MainActivity;
import com.power.using.mybomb.R;
import com.power.using.mybomb.bean.AddFriendMessage;
import com.power.using.mybomb.bean.AgreeAddFriendMessage;
import com.power.using.mybomb.bean.User;
import com.power.using.mybomb.db.NewFriend;
import com.power.using.mybomb.db.NewFriendManager;
import com.power.using.mybomb.event.RefreshEvent;
import com.power.using.mybomb.model.UserModel;
import com.power.using.mybomb.model.listener.UpdateCacheListener;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Map;

import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMMessageType;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.event.MessageEvent;
import cn.bmob.newim.event.OfflineMessageEvent;
import cn.bmob.newim.listener.BmobIMMessageHandler;
import cn.bmob.newim.notification.BmobNotificationManager;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by 聪明一只哈 on 2020/3/18.
 * Time: 2020/3/18  13:54
 */
public class DemoMessageHandler extends BmobIMMessageHandler {

    private Context context;

    public DemoMessageHandler(Context context) {
        this.context = context;
    }

    /**
     * 当接受到服务器发来的消息时，此方法被调用
     * @param messageEvent
     */
    @Override
    public void onMessageReceive(MessageEvent messageEvent) {

        executeMessage(messageEvent);
    }

    @Override
    public void onOfflineReceive(OfflineMessageEvent event) {
        //每次调用connect方法时会查询一次离线消息，如果有，此方法会被调用
        Map<String, List<MessageEvent>> map = event.getEventMap();
        Logger.i("有" + map.size() + "个用户发来离线消息");
        //挨个检测下离线消息所属的用户的信息是否需要更新
        for(Map.Entry<String,List<MessageEvent>> entry:map.entrySet()){
            List<MessageEvent> list = entry.getValue();
            int size = list.size();
            Logger.i("用户" + entry.getKey() + "发来" + size + "条消息");
            for (int i=0;i<size;i++){
                //处理每条消息
                executeMessage(list.get(i));
            }
        }




    }

    /**
     * 处理消息
     * @param messageEvent
     */
    private void executeMessage(MessageEvent messageEvent) {
        //检测用户信息是否需要更新
        UserModel.getInstance().updateUserInfo(messageEvent, new UpdateCacheListener() {
            @Override
            public void done(BmobException e) {
                BmobIMMessage msg = messageEvent.getMessage();
                Logger.i(msg.toString());
                Logger.i(msg.getExtra());
                if(BmobIMMessageType.getMessageTypeValue(msg.getMsgType())==0){
                    //自定义消息类型：0
                    processCustomMessage(msg,messageEvent.getFromUserInfo());
                }else{
                    //SDK内部支持的消息类型
                    processSDKMessage(msg,messageEvent);
                }
            }
        });

    }

    /**
     * 处理SDK支持的消息
     * @param msg
     * @param messageEvent
     */
    private void processSDKMessage(BmobIMMessage msg, MessageEvent messageEvent) {
        if(BmobNotificationManager.getInstance(context).isShowNotification()){
            //如果需要显示通知栏，SDK提供以下两种显示方式：
            Intent pendingIntent = new Intent(context, MainActivity.class);
            pendingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            //TODO 消息接收：8.5、多个用户的多条消息合并成一条通知：有XX个联系人发来了XX条消息
            //BmobNotificationManager.getInstance(context).showNotification(event, pendingIntent);

            //TODO 消息接收：8.6、自定义通知消息：始终只有一条通知，新消息覆盖旧消息
            BmobIMUserInfo info = messageEvent.getFromUserInfo();
            //这里可以是应用图标，也可以将聊天头像转成bitmap
            Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
            BmobNotificationManager.getInstance(context).showNotification(largeIcon,info.getName(),msg.getContent(),
                    "有一条消息",pendingIntent);
        }else{
            //直接发送消息事件
            EventBus.getDefault().post(messageEvent);
        }
    }

    /**
     * 处理自定义消息类型
     * @param msg
     * @param info
     */
    private void processCustomMessage(BmobIMMessage msg, BmobIMUserInfo info) {
        //消息类型
        String type = msg.getMsgType();
        //发送页面刷新广播
        EventBus.getDefault().post(new RefreshEvent());
        //处理消息
        if(type.equals(AddFriendMessage.ADD)){
            //接收到添加好友的请求
            NewFriend friend = AddFriendMessage.convert(msg);
            //本地好友请求表做下校验，本地没有的才允许显示通知栏--有可能离线消息会有些重复
            long id = NewFriendManager.getInstance(context).insertOrUpdateNewFriend(friend);
            if(id>0){
                showAddNotify(friend);
            }
        }else if(type.equals(AgreeAddFriendMessage.AGREE)){
            AgreeAddFriendMessage agree = AgreeAddFriendMessage.convert(msg);
            //添加消息的发送方为好友
            addFriend(agree.getFromId());
            //这里应该也需要做下校验--来检测下是否已经同意过该好友请求，我这里省略了
            showAddNotify(info,agree);
        }else{
            Toast.makeText(context,"接收到的滴定仪消息"+msg.getMsgType() + "," + msg.getContent() + "," + msg.getExtra(),Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 显示对方添加自己为好友的通知
     * @param friend
     */
    private void showAddNotify(NewFriend friend) {
        Intent pendingIntent = new Intent(context, MainActivity.class);
        pendingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
        //这里可以是应用图标，也可以将聊天头像转成bitmap
        Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
        BmobNotificationManager.getInstance(context).showNotification(largeIcon,
                friend.getName(), friend.getMsg(), friend.getName() + "请求添加你为朋友", pendingIntent);

    }

    /**
     * 显示对方添加自己为好友的通知
     * @param info
     * @param agree
     */
    private void showAddNotify(BmobIMUserInfo info, AgreeAddFriendMessage agree) {
        Intent pendingIntent = new Intent(context, MainActivity.class);
        pendingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
        BmobNotificationManager.getInstance(context).showNotification(largeIcon, info.getName(), agree.getMsg(), agree.getMsg(), pendingIntent);

    }


    /**
     * 收到同意添加好友后，添加好友
     * @param uid
     */
    private void addFriend(String uid) {
        User user = new User();
        user.setObjectId(uid);
        UserModel.getInstance()
                .agreeAddFriend(user, new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if(e==null){
                            Logger.e("success");
                        }else{
                            Logger.e(e.getMessage());
                        }
                    }
                });
    }

}
